﻿using MusicPlayerPlus.Interface.Model.Muisc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;

namespace MusicPlayerPlus.PlugIn.DownloadMusic.Model
{
    public class DownloadMusicItem : MusicItem, INotifyPropertyChanged
    {
        public DownloadMusicItem(MusicItem track)
        {
            base.ID = track.ID;
            base.Name = track.Name;
            base.AlbumName = track.AlbumName;
            base.Location = track.Location;
            base.Lyric = track.Lyric;
            base.Cove = track.Cove;
            base.CoveHD = track.CoveHD;
            base.CoveThumbnail = track.CoveThumbnail;
            base.Url = track.Url;
            base.Type = track.Type;
        }

        #region MyRegion

        /// <summary>
        /// 本地的mp3地址
        /// </summary>
        public string LocaMp3Path
        {
            get
            {
                string ext = System.IO.Path.GetExtension(this.Url);
                string title = this.Name.Replace("\\", "_");
                title = title.Replace("/", "_");
                return AMing.Core.IO.Path.GetApplicationPath(string.Format("/Musics/{0}{1}", title, ext));
            }
        }

        /// <summary>
        /// 本地的mp3是否存在
        /// </summary>
        public bool LocaMp3Exist
        {
            get
            {
                return System.IO.File.Exists(this.LocaMp3Path);
            }
        }

        #endregion


        #region NotifyPropertyChanged

        private int downloadProgressPercentage;
        /// <summary>
        /// 下载进度百分比
        /// </summary>
        public int DownloadProgressPercentage
        {
            get { return downloadProgressPercentage; }
            set
            {
                if (this.downloadProgressPercentage != value)
                {
                    downloadProgressPercentage = value;
                    NotifyPropertyChanged("DownloadProgressPercentage");
                }
            }
        }

        private long bytesReceived;
        /// <summary>
        /// 收到的字节数
        /// </summary>
        public long BytesReceived
        {
            get { return bytesReceived; }
            set
            {
                if (this.bytesReceived != value)
                {
                    bytesReceived = value;
                    NotifyPropertyChanged("BytesReceived");
                }
            }
        }

        private long totalBytesToReceive;
        /// <summary>
        /// 总的字节数
        /// </summary>
        public long TotalBytesToReceive
        {
            get { return totalBytesToReceive; }
            set
            {
                if (this.totalBytesToReceive != value)
                {
                    totalBytesToReceive = value;
                    if (totalBytesToReceive > 0)
                    {
                        this.DownloadUnStart = false;
                    }
                    NotifyPropertyChanged("TotalBytesToReceive");
                }
            }
        }

        private string downloadCompleted = "正在下载...";
        /// <summary>
        /// 下载是否完成
        /// </summary>
        public string DownloadCompleted
        {
            get { return downloadCompleted; }
            set
            {
                if (this.downloadCompleted != value)
                {
                    downloadCompleted = value;
                    NotifyPropertyChanged("DownloadCompleted");
                }
            }
        }

        private bool downloadUnStart = true;
        /// <summary>
        /// 下载是否还没开始
        /// </summary>
        public bool DownloadUnStart
        {
            get { return downloadUnStart; }
            set
            {
                if (this.downloadUnStart != value)
                {
                    downloadUnStart = value;
                    NotifyPropertyChanged("DownloadUnStart");
                }
            }
        }

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                  new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;


        #endregion

        #region Method
        /// <summary>
        /// 下载
        /// </summary>
        public void Download()
        {
            if (this.LocaMp3Exist)
            {
                this.DownloadCompleted = "Mp3文件已经存在！";
                this.DownloadUnStart = false;
                return;
            }
            AMing.Core.Http.DownloadHelper downloadHelper = new AMing.Core.Http.DownloadHelper();
            downloadHelper.Download_Async(this.Url, this.LocaMp3Path,
            (dpp) =>
            {
                this.DownloadProgressPercentage = dpp;
            },
            (dp, tdp) =>
            {
                this.BytesReceived = dp;
                this.TotalBytesToReceive = tdp;
            },
            (isCompleted) =>
            {
                this.DownloadCompleted = isCompleted ? "下载成功！" : "下载失败！";
            });
        }

        #endregion
    }
}
